//**********************************************************
//    File Name: AvailableTimeSlot.cs
//    Author: Team Motif
//    Date Created : 04/13/2007
//    Current Owner: Team Motif 
//                                                                            
//    Purpose: 
//    This class handles a particular slot of time - with a begin time 
//    and an end time. All functions that manipulate a slot of time are placed 
//    in this class.
//	  Copyright 2007
//**********************************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Library
{
    public class AvailableTimeSlot
    {
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Default constructor for AvailableTimeSlot
        /// -------------------------------------------------------------------------------
        /// </summary>
        public AvailableTimeSlot()
        {
            mBeginTime = new DateTime();
            mEndTime = new DateTime();
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Constructor for AvailableTimeSlot
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        public AvailableTimeSlot(DateTime startTime, DateTime endTime)
        {
            mBeginTime = startTime;
            mEndTime = endTime;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the start time
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public DateTime GetBeginDateTime()
        {
            return mBeginTime;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Sets the start time
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="time"></param>
        public void SetBeginDateTime(DateTime time)
        {
            mBeginTime = time;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the end time
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public DateTime GetEndDateTime()
        {
            return mEndTime;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Sets the end time
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="time"></param>
        public void SetEndDateTime(DateTime time)
        {
            mEndTime = time;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the day of the week
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public DayOfWeek GetDay()
        {
            //assuming the Start Day and End day are the same - have to check for this later
            return mBeginTime.DayOfWeek;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the start hour
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public int GetStartHour()
        {
            return mBeginTime.Hour;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the start minute
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public int GetStartMinute()
        {
            return mBeginTime.Minute;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the start second
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public int GetStartSecond()
        {
            return mBeginTime.Second;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the end hour
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public int GetEndHour()
        {
            return mEndTime.Hour;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the end minute
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public int GetEndMinute()
        {
            return mEndTime.Minute;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the end second
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public int GetEndSecond()
        {
            return mEndTime.Second;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the date of the appointment
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public DateTime GetDate()
        {
            //assuming the Start Date and End date are the same - have to check for this later
            return mBeginTime.Date;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the start date
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public DateTime GetBeginDate()
        {
            return mBeginTime.Date;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the end date
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public DateTime GetEndDate()
        {
            return mBeginTime.Date;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Get the available slots of the users from the database for a particular client
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="ClientID"></param>
        /// <returns></returns>
        public static ArrayList GetAvailDetailsForClient(long ClientID, DateTime szDate, DateTime szNextdate)
        {
            ArrayList myTimeslots = new ArrayList();
            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                myTimeslots = DBUtil.ReadAvailDetailsForClient(ClientID, szDate, szNextdate);
            }
            return myTimeslots;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Given a date for the start of a week and a particular day of the week, this function returns
        /// the date for that day.
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="weekStartDate"></param>
        /// <param name="dayOfWeek"></param>
        /// <returns></returns>
        public static DateTime GetDateForDay(DateTime weekStartDate, string dayOfWeek)
        {
            DateTime dateForDay = new DateTime();
            dateForDay = weekStartDate;

            DayOfWeek weekStartDay = weekStartDate.DayOfWeek;

            DayOfWeek availDay = new DayOfWeek();

            switch (dayOfWeek)
            {
                case "Monday": availDay = DayOfWeek.Monday;
                    break;
                case "Tuesday": availDay = DayOfWeek.Tuesday;
                    break;
                case "Wednesday": availDay = DayOfWeek.Wednesday;
                    break;
                case "Thursday": availDay = DayOfWeek.Thursday;
                    break;
                case "Friday": availDay = DayOfWeek.Friday;
                    break;
                case "Saturday": availDay = DayOfWeek.Saturday;
                    break;
                case "Sunday": availDay = DayOfWeek.Sunday;
                    break;
                    //Need to throw expection for default case.
            }
            int numberOfDays = 0;
            if (availDay > weekStartDay)
            {
                numberOfDays = availDay - weekStartDay;
            }
            else if (availDay < weekStartDay)
            {
                numberOfDays = availDay - weekStartDay + 7;
            }
            
            dateForDay = dateForDay.AddDays(numberOfDays);

            return dateForDay;
        }

        // -------------------------------------------------------------------------------
        // Private data region
        // -------------------------------------------------------------------------------
        DateTime mBeginTime;
        DateTime mEndTime;
        
        static Utility DBUtil = new Utility();
    }
}
